package com.vincent.filepicker.ability;

import com.vincent.filepicker.Constant;
import com.vincent.filepicker.ResourceTable;
import com.vincent.filepicker.adapter.FolderListAdapter;
import com.vincent.filepicker.adapter.NormalFilePickAdapter;
import com.vincent.filepicker.adapter.OnSelectStateListener;
import com.vincent.filepicker.filter.FileFilter;
import com.vincent.filepicker.filter.callback.FilterResultCallback;
import com.vincent.filepicker.filter.entity.Directory;
import com.vincent.filepicker.filter.entity.NormalFile;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Vincent Woo
 * Date: 2016/10/26
 * Time: 10:14
 */

public class NormalFilePickAbility extends BaseAbility {
    public static final int DEFAULT_MAX_NUMBER = 9;
    public static final String SUFFIX = "Suffix";
    private int mMaxNumber;
    private int mCurrentNumber = 0;
    private ListContainer mRecyclerView;
    private NormalFilePickAdapter mAdapter;
    private ArrayList<NormalFile> mSelectedList = new ArrayList<>();
    private List<Directory<NormalFile>> mAll;
    private ProgressBar mProgressBar;
    private String[] mSuffix;

    private Text tv_count;
    private Text tv_folder;
    private DirectionalLayout ll_folder;
    private DependentLayout rl_done;
    private DependentLayout tb_pick;

    @Override
    public void permissionGranted() {
        loadData();
    }

    @Override
    public void permissionDenied() {

    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_vw_ability_file_pick);

        mMaxNumber = getIntent().getIntParam(Constant.MAX_NUMBER, DEFAULT_MAX_NUMBER);
        mSuffix = getIntent().getStringArrayParam(SUFFIX);
        initView();
    }

    private void initView() {
        tv_count = (Text) findComponentById(ResourceTable.Id_tv_count);
        tv_count.setText(mCurrentNumber + "/" + mMaxNumber);

        mRecyclerView = (ListContainer) findComponentById(ResourceTable.Id_rv_file_pick);
        mAdapter = new NormalFilePickAdapter(this, mMaxNumber);
        mRecyclerView.setItemProvider(mAdapter);

        mAdapter.setOnSelectStateListener(new OnSelectStateListener<NormalFile>() {
            @Override
            public void OnSelectStateChanged(boolean state, NormalFile file) {
                if (state) {
                    mSelectedList.add(file);
                    mCurrentNumber++;
                } else {
                    mSelectedList.remove(file);
                    mCurrentNumber--;
                }
                tv_count.setText(mCurrentNumber + "/" + mMaxNumber);
            }
        });

        mProgressBar = (ProgressBar) findComponentById(ResourceTable.Id_pb_file_pick);

        rl_done = (DependentLayout) findComponentById(ResourceTable.Id_rl_done);
        rl_done.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                Intent intent = new Intent();
                intent.setSequenceableArrayListParam(Constant.RESULT_PICK_FILE, mSelectedList);
                setResult(RESULT_OK, intent);
                terminateAbility();
            }
        });

        tb_pick = (DependentLayout) findComponentById(ResourceTable.Id_tb_pick);
        tb_pick.findComponentById(ResourceTable.Id_rl_back).setClickedListener(component -> terminateAbility());
        ll_folder = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_folder);
        if (isNeedFolderList) {
            ll_folder.setVisibility(Component.VISIBLE);
            ll_folder.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                    mFolderHelper.toggle(tb_pick);
                }
            });
            tv_folder = (Text) findComponentById(ResourceTable.Id_tv_folder);
            tv_folder.setText(getString(ResourceTable.String_vw_all));

            mFolderHelper.setFolderListListener(new FolderListAdapter.FolderListListener() {
                @Override
                public void onFolderListClick(Directory directory) {
                    mFolderHelper.toggle(tb_pick);
                    tv_folder.setText(directory.getName());

                    if (null == directory.getPath() || directory.getPath().isEmpty()) { //All
                        refreshData(mAll);
                    } else {
                        for (Directory<NormalFile> dir : mAll) {
                            if (dir.getPath().equals(directory.getPath())) {
                                List<Directory<NormalFile>> list = new ArrayList<>();
                                list.add(dir);
                                refreshData(list);
                                break;
                            }
                        }
                    }
                }
            });
        }
    }

    private void loadData() {
        FileFilter.getFiles(this, new FilterResultCallback<NormalFile>() {
            @Override
            public void onResult(List<Directory<NormalFile>> directories) {
                // Refresh folder list
                if (isNeedFolderList) {
                    ArrayList<Directory> list = new ArrayList<>();
                    Directory all = new Directory();
                    all.setName(getString(ResourceTable.String_vw_all));
                    list.add(all);
                    list.addAll(directories);
                    mFolderHelper.fillData(list);
                }

                mAll = directories;
                refreshData(directories);
            }
        }, mSuffix);
    }

    private void refreshData(List<Directory<NormalFile>> directories) {
        mProgressBar.setVisibility(Component.HIDE);
        List<NormalFile> list = new ArrayList<>();
        for (Directory<NormalFile> directory : directories) {
            list.addAll(directory.getFiles());
        }

        for (NormalFile file : mSelectedList) {
            int index = list.indexOf(file);
            if (index != -1) {
                list.get(index).setSelected(true);
            }
        }

        mAdapter.refresh(list);
    }

}
